package com.jingdianjichi.subject.controller;

import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.controller.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.controller.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.controller.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.controller.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 标签Controller
 */
@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {
    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;

    /**
     * 新增分类
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO) {

        try {
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);
            Boolean add = subjectLabelDomainService.add(subjectLabelBO);
            log.info("添加成功");
            return Result.success(add);
        } catch (Exception e) {
            log.info("添加任务时失败，捕获异常");
            return Result.fail("添加失败");
        }



    }

    /**
     * 更新分类
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO) {

        try {
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);
            Boolean result = subjectLabelDomainService.update(subjectLabelBO);
            log.info("更新成功");
            return Result.success(result);
        } catch (Exception e) {
            log.info("添加任务时失败，捕获异常");
            return Result.fail("更新失败");
        }



    }

    /**
     * 删除标签
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO) {

        try {
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);
            Boolean result = subjectLabelDomainService.delete(subjectLabelBO);
            log.info("删除成功");
            return Result.success(result);
        } catch (Exception e) {
            log.info("删除任务时失败，捕获异常");
            return Result.fail("删除失败");
        }

    }

    /**
     * 查询分类下标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectCategoryDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {


            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);
            List<SubjectLabelBO> result = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);
            List<SubjectLabelDTO> subjectLabelDTOS = SubjectLabelDTOConverter.INSTANCE.convertBOToLabelDTOList(result);
            log.info("查询成功");
            return Result.success(subjectLabelDTOS);


    }


}
